package errors

// 应用错误
type AppError struct {
	Msg      string `json:"msg"`
	Code     string `json:"code"`
	SubError error  `json:"-"`
}

func (a AppError) Error() string {
	if a.Code != "" {
		return a.Code + ":" + a.Msg
	}
	return a.Msg
}

func NewAppError(err error, code string) AppError {
	return AppError{
		Msg:  err.Error(),
		Code: code,
	}
}

func WrapAppError(msg string, code string, subError error) AppError {
	return AppError{
		Msg:      msg,
		Code:     code,
		SubError: subError,
	}
}

// 设置错误消息
func (e AppError) SetMsg(msg string) AppError {
	e.Msg = msg
	return e
}

// 设置错误编码
func (e AppError) SetCode(code string) AppError {
	e.Code = code
	return e
}
